#include "tcp_srv_socket.h"

#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>


namespace tinySrv
{

  TCPSrvSocket::TCPSrvSocket(std::string const &localAddress, unsigned short localPort, int backlog)
    : TCPSocketHolder(false)
  {
    sockaddr_in Address = { 0 };
    Address.sin_family = AF_INET;
    Address.sin_addr.s_addr = inet_addr(localAddress.c_str());
    Address.sin_port = htons(localPort);
    if (bind(GetSocket(), reinterpret_cast<sockaddr const *>(&Address), sizeof(Address)) == -1)
      throw TCPSrvSocketException("Can't bind socket", errno);
    if (listen(GetSocket(), backlog) == -1)
      throw TCPSrvSocketException("Can't listen socket", errno);
  }

}
